<template>
	<view>
		<view class="bg-fff padding-30">
			<u-search placeholder="搜索" v-model="keyword"></u-search>
		</view>
		<view class="padding-30">
			<view class="flex-alignItems-center padding-30 bg-fff border-radius20 margin-bottom30"
				v-for="(item,index) in list" :key="index" @click="handleSelect(item)">
				<view class="margin-right20">
					<image class="img-44" v-if="item.isSelect" src="../../static/image/checked.png" mode=""></image>
					<image class="img-44" v-else src="../../static/image/check.png" mode=""></image>
				</view>
				<view class="flex-alignItems-center">
					<image class="img-100 border-50radius margin-right20" :src="item.avatar" mode="">
					</image>
					<view class="">
						<view class="fontSize-16 color-000 fontWeight-bold margin-bottom10">{{item.nickname}}</view>
						<view class="fontSize-15 color-999">{{item.keshi}}</view>
					</view>
				</view>
			</view>
			<u-empty v-if="list.length == 0" mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png"
				width="150px" height="150px" text="列表为空" textSize="18">
			</u-empty>
			<view style="padding-bottom: 100rpx;">
				<u-loadmore v-if="loading" status="loading" />
				<u-loadmore v-if="isLastPage" status="nomore" />
			</view>
		</view>

		<!-- 底部操作栏 -->
		<view style="height: 200rpx;"></view>
		<view class="position-fixed-bottom padding-30 bg-fff" @click="SubmitEvent">
			<view class="btn50-0165FD-15ffffff flex-center">
				绑定
			</view>
		</view>

		<!-- 绑定提示 -->
		<u-popup :show="doctorShow" :round="20" mode="center" :safeAreaInsetBottom="true">
			<view class="jiuzhen-popup-view">
				<view class="fontSize-16 color-000 fontWeight-bold margin-bottom30 text-center">提示</view>
				<view class="fontSize-15 color-000 text-center">绑定成功后7日内可修改1次，之后不再支持修改</view>
				<view class="flex-center margin-top50">
					<view class="jiuzhen-popup-btn flex-center margin-right20" @click="jiuzhenShow = false">取消</view>
					<view class="jiuzhen-popup-btn1 flex-center">去添加</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import { mapMutations } from 'vuex'
	export default {
		data() {
			return {
				keyword: '',
				doctorShow: '',
				page: 1,
				limit: 10,
				list: [],
				isLastPage: false,
				loading: false,
				// 选中对象
				selectItem: {}
			}
		},
		onShow() {
			this.page = 1
			this.limit = 10
			this.list = []
			this.getDoctorList()
		},
		// 刷新 上拉刷新
		onPullDownRefresh() {
			this.page = 1
			this.isLastPage = false
			this.list = []
			this.getDoctorList().finally(() => {
				// 停止当前页面下拉刷新
				uni.stopPullDownRefresh();
			});
		},
		// 加载下一页 下拉加载
		onReachBottom() {
			if (this.list.length >= this.page * this.limit) {
				this.isLastPage = false;
				this.page += 1;
				this.getDoctorList();
			} else {
				this.isLastPage = true
			}
		},
		methods: {
			...mapMutations(['petSelectList']),
			// 绑定
			SubmitEvent() {
				this.petSelectList(this.selectItem)
				this.$tools.navigateBack()
			},
			// 单选 选择医生
			handleSelect(item) {
				// 判断是否有选中项
				if (this.selectItem.id) {
					// 判断是否是已选择
					if (item.id == this.selectItem) {
						item.isSelect = false
						this.selectItem = {}
					} else {
						this.list.forEach(item1 => {
							if (item1.id == item.id) {
								item1.isSelect = true
								this.selectItem = item
							} else {
								item1.isSelect = false
							}
						})
					}
				} else {
					item.isSelect = true
					this.selectItem = item
				}
			},
			// 医生列表
			async getDoctorList() {
				let res = await this.$http.post(this.$api.doctor_list, {
					page: this.page,
					limit: this.limit
				}, {
					token: true
				})
				let list = res.data.map(item => {
					return {
						id: item.id,
						nickname: item.nickname,
						avatar: item.avatar,
						keshi: item.keshi,
						isSelect: false
					}
				})
				this.list = [...this.list, ...list]
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>